export default class accelerometer{
  constructor(){ 
    this.enabled = false
    this.interval = "game" //ui game normal
    this.data = {x : 0, y : 0, z : 0}
  }
  startSuccess(){ }
  startFail() { }
  startComplete () {}

  stopSuccess() { }
  stoptFail() { }
  stopComplete() { }


  change(data) { 
    this.data.dx = data.x - this.data.x
    this.data.x = data.x
    this.data.dy = data.y - this.data.y
    this.data.y = data.y
    this.data.dz = data.z - this.data.z
    this.data.z = data.z
    this.data.changed = 
      this.data.dx == 0 && 
      this.data.dy == 0 && 
      this.data.dz == 0
    if (this.data.changed){
      this.onChanged.bind(this)()
    }
  }
  start(){
    wx.startAccelerometer({
      interval : this.interval,
      success : this.startSuccess.bind(this),
      fail : this.startFail.bind(this),
      complete : this.startComplete.bind(this)
    })
    wx.onAccelerometerChange(this.change.bind(this))
    this.enabled = true
  }
  stop(){
    wx.stopAccelerometer({
      success: this.stopSuccess.bind(this),
      fail: this.stopFail.bind(this),
      complete: this.stopComplete.bind(this)
    })
    this.enabled = false
  }
  onChanged(){}
}